package com.zaofan.mobile.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.AppyOpen;
import com.zaofan.constant.StateConstant;
import com.zaofan.exception.ServiceException;
import com.zaofan.service.AppyOpenService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

@WebServlet("/mobile/appy")
public class AppyOpenController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private AppyOpenService appyOpenService = new AppyOpenService();

	protected void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			params.put("state", StateConstant.APPY_STATE_OK);
			List<AppyOpen> appyOpens = appyOpenService.findAll(params);
			Long count = appyOpenService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", appyOpens, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	/**
	 * 开通相关 验证码接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void code(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String mobile = request.getParameter("mobile");
			String code = WebUtils.getRandonNum(4);
			appyOpenService.sendValidateCode(mobile, code);
			request.getSession().setAttribute("code", code);
			request.getSession().setAttribute("mobile", mobile);
			System.err.println("appy send code : " + code);
			JsonUtils.writeSuccessJson("发送成功", code, response);
			return;
		} catch (ServiceException service) {
			JsonUtils.writeFailJson(service.getMessage(), response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("发送失败", response);
		}
		JsonUtils.writeFailJson("发送失败", response);
	}

	protected void get(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			AppyOpen appyOpen = appyOpenService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", appyOpen, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

	protected void submit(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			AppyOpen appyOpen = WebUtils.write2bean(request, AppyOpen.class);
			appyOpen.setState(StateConstant.APPY_STATE_CHECKED);
			String code = request.getParameter("code");
			String old = (String) request.getSession().getAttribute("code");
			String mobile = (String) request.getSession()
					.getAttribute("mobile");
			if (code == null) {
				JsonUtils.writeBeanFailJson("请填写正确的验证码!!", response);
				return;
			}
			if (old == null) {
				JsonUtils.writeBeanFailJson("验证码过期,请重试!!", response);
				return;
			}
			if (!code.equals(old)) {
				JsonUtils.writeBeanFailJson("验证码错误,请重试!!", response);
				return;
			}
			if (!mobile.equals(appyOpen.getMobile())) {
				JsonUtils.writeBeanFailJson("手机号码不一致,请重试!!", response);
				return;
			}
			appyOpenService.addUserAppy(appyOpen);
			JsonUtils.writeSuccessJson("操作成功", appyOpen, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("操作失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("操作失败!!", response);
	}

	protected void submit1(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			AppyOpen appyOpen = WebUtils.write2bean(request, AppyOpen.class);
			appyOpen.setState(StateConstant.APPY_STATE_CHECKED);
			appyOpenService.addUserAppy(appyOpen);
			JsonUtils.writeSuccessJson("操作成功", appyOpen, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("操作失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("操作失败!!", response);
	}

	protected void appy(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String appyId = request.getParameter("id");
			String mobile = request.getParameter("mobile");
			String code = request.getParameter("code");
			String old = (String) request.getSession().getAttribute("code");
			String oldMobile = (String) request.getSession().getAttribute(
					"mobile");
			if (appyId == null) {
				JsonUtils.writeBeanFailJson("请填写正确的参数!!", response);
				return;
			}
			if (code == null) {
				JsonUtils.writeBeanFailJson("请填写正确的验证码!!", response);
				return;
			}
			if (mobile == null) {
				JsonUtils.writeBeanFailJson("请填写正确的手机号码!!", response);
				return;
			}
			if (old == null) {
				JsonUtils.writeBeanFailJson("验证码过期,请重试!!", response);
				return;
			}
			if (oldMobile == null) {
				JsonUtils.writeBeanFailJson("手机号码错误,请重试!!", response);
				return;
			}

			if (!code.equals(old)) {
				JsonUtils.writeBeanFailJson("验证码错误,请重试!!", response);
				return;
			}
			if (!mobile.equals(oldMobile)) {
				JsonUtils.writeBeanFailJson("手机号码不一致,请重试!!", response);
				return;
			}
			appyOpenService.appy(appyId, mobile);
			JsonUtils.writeSuccessJson("参与成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("参与失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("参与失败!!", response);
	}

	protected void appy1(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String appyId = request.getParameter("id");
			String mobile = request.getParameter("mobile");
			appyOpenService.appy(appyId, mobile);
			JsonUtils.writeSuccessJson("参与成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("参与失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("参与失败!!", response);
	}

	/**
	 * 申请 开通区域 查找 列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void search(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String key = request.getParameter("key");
			if (key == null) {
				JsonUtils.writeListFailJson("请输入需要查找的信息", response);
				return;
			}
			List<AppyOpen> opens = appyOpenService.findByNameLike(key);
			JsonUtils.writeSuccessJson("查询成功", opens, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

}
